<?php
class User extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('user_m','user');
		$this->load->library(array('result','form_validation'));
		$this->load->helper('form');
	}
	
	function register(){
		$sex=$this->input->post('sex');
		$phone=$this->input->post('phone');
		$email=$this->input->post('email');
		$token=$this->input->post('token');
		
		if(!empty($token)){
			$token=base64_encode($phone+$email+time());
		}
		$data=array(
			'phone'=>$phone,
			'email'=>$email,
			'gender'=>$sex,
			'role_id'=>1,
			'token'=>$token
		);
		$insertid=$this->user->insert($data);
		if($insertid>0){
			$this->result->setData(0,'注册成功',null);
		}else{
			$this->result->setData(1,'注册失败',null);
		}
		$this->result->output();
	}
	
	function queryEmail(){
		$phone=$this->uri->segment(3);
		$row=$this->user->queryUserByPhone($phone);
		if(isset($row)){
			$this->result->setData(0,'查找该用户成功',array('email'=>$row->email));
		}else{
			$this->result->setData(1,'没有找到该用户邮箱',null);
		}
		$this->result->output();
	}
	
	function login(){
		$phone=$this->input->post('phone');
		$email=$this->input->post('email');
		$pwd=$this->input->post('pwd');

		$data=array();
		if(!empty($phone)){
			$data['phone']=$phone;
		}else if(!empty($email)){
			$data['email']=$email;
		}else {
			$this->result->setData(1,'账号不能为空',null);
			$this->result->output();
			return;
		}
		if(!empty($pwd)){
			$data['pwd']=$pwd;
		}else{
			$this->result->setData(1,'密码不能为空',null);
			$this->result->output();
			return;			
		}
		$row=$this->user->query($data);
		if(!empty($row)){
			$this->result->setData(0,'登陆成功',$row[0]);
		}else{
			$this->result->setData(1,'账号或密码不正确',null);
		}
		$this->result->output();

	}
	
	function sendEmail(){
		$this->load->library('mysmtp');
		
		//$email=$this->input->post('email');
		$email='85579007@qq.com';
		$token='11111111111';
		
		$smtpserver = "smtp.126.com";//SMTP服务器
		$smtpserverport =25;//SMTP服务器端口
		$smtpusermail = "zjkw123@126.com";//SMTP服务器的用户邮箱
		$smtpemailto = "85579007@qq.com";//发送给谁
		$smtpuser = "zjkw123@126.com";//SMTP服务器的用户帐号
		$smtppass = "zjkw123";//SMTP服务器的用户密码
		$mailtitle = "密码重置邮件";//邮件主题
		$mailcontent = '<a href="http://localhost/ci/index.php/user/resetpwd/'.$token.'">点击重置密码</a>';//邮件内容
		$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
	//************************ 配置信息 ****************************
	
		$this->mysmtp->smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
		//$smtp->debug = false;//是否显示发送的调试信息
		$state = $this->mysmtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
		
		if($state){
			$this->result->setData(0,'邮件发送成功',null);
		}else{
			$this->result->setData(1,'邮件发送失败',null);
		}
		$this->result->output();
	}
	
	function resetPwd(){
		$token=$this->uri->segment(3);
		
		$this->form_validation->set_rules('newpwd','Password','required');
		$this->form_validation->set_rules('repwd','Repassword','required');
		
		if(empty($token)){
			$token=$this->input->post('token');
		}
		
		if($this->form_validation->run()==false){
			$this->load->view('resetpwd',array('token'=>$token));
		}else{
			$token=$this->input->post('token');
			$data=array(
				'pwd'=>$this->input->post('newpwd')
			);
			$this->user->update($data,$token);
			$this->load->view('success',array('msg','重置密码成功'));
		}
	}
	
	function upInfo(){
		log_message('info','upinfo');
		
		$token=$this->input->post('token');
		
		$photo=$this->input->post('photourl');
		$name=$this->input->post('name');
		$nick=$this->input->post('nick');
		$sex=$this->input->post('sex');
		$roleid=$this->input->post('role_id');
		$school=$this->input->post('school');
		$department=$this->input->post('department');
		$grade=$this->input->post('grade');
		$year=$this->input->post('year');
		log_message('info','name='.$name);
		log_message('info','nick='.$nick);
		log_message('info','sex='.$sex);
		$data=array();
		if(!empty($photo)){
			log_message('info',"photo ok");
			$this->load->helper('file');
			$this->load->helper('url');
			$path='./';
			$fname=time().'.jpg';
			if(!write_file($path.'/'.$fname,$photo)){
				$photourl="";
			}else{
				$photourl=base_url($path.$fname);
			}
			$data['photourl']=$photourl;
		}
		if(!empty($name)){
			$data['name']=$name;
		}
		if(!empty($nick)){
			$data['nick']=$nick;
		}
		if($sex==0||$sex==1){
			$data['gender']=$sex;
		}
		if(!empty($roleid)){
			$data['role_id']=$roleid;
		}
		if(!empty($school)){
			$data['school']=$school;
		}
		if(!empty($department)){
			$data['department']=$department;
		}
		if(!empty($grade)){
			$data['grade']=$grade;
		}
		if(!empty($year)){
			$data['year']=$year;
		}
		log_message('info',print_r($data,true));
		if(!empty($data)){
			if($this->user->update($data,$token)>0){
				$row=$this->user->query(array('token'=>$token));
				$this->result->setData(0,'更新成功',$row[0]);
			}else{
				$this->result->setData(1,'更新失败',null);
			}
			$this->result->output();
		}
	}
}